package edu.noteBook.controller;

import edu.noteBook.Pojo.Title;
import edu.noteBook.service.TitleService;
import edu.noteBook.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/title")
@Validated
@CrossOrigin
public class TitleController {
    @Autowired
    private TitleService titleService;

    /**
     * 查询所有标题
     * @return List<Title>
     */
    @GetMapping
    public R queryAllTitle() {
//        System.out.println(titleService.queryAllTitle());
        return new R(true, titleService.queryAllTitle());
    }

    /**
     * 根据标题查询
     * @param title
     * @return List<Title>
     */
    @GetMapping("/{title}")
    public R queryTitle(@PathVariable("title") String title) {
        System.out.println(titleService.queryTitle(title));
        return new R(true, titleService.queryTitle(title));
    }

    /**
     * 添加标题
     * @param title
     * @return boolean
     */
    @PostMapping("/{title}")
    public R addTitle(@PathVariable("title") String title) {
        List<Title> title1 = titleService.queryTitle(title);
        if (!title1.isEmpty()) {
            return new R(false, "添加失败，该标题已存在");
        }else {
            return new R(true, titleService.addTitle(title));
        }

    }

    /**
     * 删除标题
     * @param id
     * @return boolean
     */
    @DeleteMapping("/{id}")
    public R deleteTitle(@PathVariable("id") Integer id) {
        return new R(true, titleService.deleteTitle(id));
    }

    /**
     * 修改标题
     * @param title
     * @param id
     * @return boolean
     */
    @PutMapping("/{title}/{id}")
    public R updateTitle(@PathVariable("title") String title,@PathVariable("id") Integer id) {
        return new R(true, titleService.updateTitle(title, id));
    }
}
